﻿using System;
using System.Data;
using olierFrm.WebFormDev;
using olierFrm.DataBase.FrameWork.StdQuery;
using olierFrm.WebFormDev.StdQuery;
using olierFrm.WebFormDev.Cookie;
using olierFrm.Extensions.EDataTable;
namespace WebFormDev.Plugin
{
    public partial class DialogGridPage : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            ManageSystem.IsLogin();
            ManageSystem.SetTheme(Page);
            hfThemeName.Value = CTheme.Theme;
            //没有QueryName只显示菜单，为了重复利用该页面（系统首页和StdQuery）

        }

        protected void Page_Load(object sender, EventArgs e)
        {

            var dbStdQuery = new StdQuerySQL(StdQuery.QueryName);


            foreach (var p in dbStdQuery.StdQueryParams)
            {
                if (p.ParamType == "EndDate" && Request.QueryString[p.ParamName] != null && Request.QueryString[p.ParamName] == "")
                    dbStdQuery.SetParam(p.ParamName, "2100-1-1");
                else if (p.ParamType == "Date" && Request.QueryString[p.ParamName] != null && Request.QueryString[p.ParamName] == "")
                    dbStdQuery.SetParam(p.ParamName, "1900-1-1");
                else dbStdQuery.SetParam(p.ParamName, Request.QueryString[p.ParamName]);
            }
            dbStdQuery.AddSystemParams();

            //var su = (SessionUser)Session[SessionUser.SessionID];

            DataTable dtSource;
            try
            {
                dtSource = dbStdQuery.ExecuteSQL();
            }
            catch (Exception ex)
            {
                Response.Clear();
                Response.Write(ex.Message);
                Response.End();
                return;
            }
            dtSource.AddDataRowIndex();
            StdQuerySource sss = new StdQuerySource(dtSource, dbStdQuery);
            var gridMaster = sss.CreateGridView("DialogGrid");
            gridMaster.KeyFieldName = dtSource.GetDataRowIndexFieldName();

            string CallBackField = "";
            foreach (DataColumn dc in dtSource.Columns)
            {
                CallBackField += dc.ColumnName + ";";
            }
            if (CallBackField.Length != 0)
                CallBackField = CallBackField.Substring(0, CallBackField.Length - 1);

            string IsDataObject = "";
            if (Request.QueryString["IsDataObject"] == "1")
            {
                IsDataObject = "parent.$('#hfCallBackField').val('" + CallBackField + "');";
            }
            gridMaster.ClientSideEvents.Init = "function(s,e){" + IsDataObject + "}";
            gridMaster.ClientSideEvents.EndCallback = "function(s,e){ResizeGrid();InitGrid();DialogGrid.UnselectAllRowsOnPage();" + IsDataObject + "}";
            gridMaster.ClientSideEvents.RowClick = "function(s,e){DialogGrid.SelectAllRowsOnPage(false);DialogGrid.SelectRow(e.visibleIndex, true);}";
            this.form1.Controls.Add(gridMaster);
        }
    }
}